package AudibleTest;
/**
 * This is Sachin. Just learning more stuff.
 * 
 * @author Sachin Takkar..
 * 
 */
public class AudibleTest {

	/**
	 * Testing with Main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = new int[] { 1, 2, 3, 4, 5 };
		print(multiplyAlgo(array));

		System.out.println("  ");

		array = new int[] { 0, 20, 5, 10, 9 };
		print(multiplyAlgo(array));
	}

	/**
	 * Prints the Array.
	 * 
	 * @param array
	 */
	private static void print(int[] array) {
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + "	");
		}
	}

	/**
	 * This method returns an array of numbers finalArray, where finalArray[i]
	 * is the product of all numbers in input array excluding input[i];
	 * 
	 * @param array
	 * @return
	 */
	public static int[] multiplyAlgo(int[] input) {
		if (input == null || input.length == 1) {
			return null;
		}

		int multiply = 1, i;
		int arrayLength = input.length;
		int[] finalArray = new int[arrayLength];

		for (i = 0; i < arrayLength; i++) {
			finalArray[i] = multiply;
			multiply = multiply * input[i];
		}

		multiply = 1;
		for (i = arrayLength - 1; i >= 0; i--) {
			finalArray[i] = finalArray[i] * multiply;
			multiply = multiply * input[i];
		}

		return finalArray;
	}

}
